package com.atguigu.gulimall.order.config;

import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayTradePagePayRequest;
import com.atguigu.gulimall.order.vo.PayVo;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

@ConfigurationProperties(prefix = "alipay")
@Component
@Data
public class AlipayTemplate {

    //在支付宝创建的应用的id
    private   String app_id = "2021000119609268";

    // 商户私钥，您的PKCS8格式RSA2私钥
    private  String merchant_private_key = "MIIEvgIBADANBgkqhkiG9w0BAQEFAASCBKgwggSkAgEAAoIBAQCT+8LsSmsph94C5NG/yD/+fO8iFMk2mIhyxsMKSEdj0iQnLoYurtPCsBYhS9wZNgPalP7RQOKX+6RPWW4H4sU4vloygryJBs6QLZRD/ND6GqspyX7bFfN//cedP78p7Nca4SZlEWxGxTuL7ITU2Urzf5aKUWnMv9OempCjXrTCtfRez5UdgcmkihtUuOa7q5gyI3SQRKDsOpEMZL1FPs18JWrkqazGLNkvZfPESpzjhnJdD4UMllzbdC2+ThD4afUgf5+QjcueXiPqVJERxo7XSrsal+cqfgqjKU7RU8wJqiFeFyZ//ELutN2DPVWUGy8efV0NtPZXLwf0Le2yNJ1RAgMBAAECggEBAInlBv0CV5tKlPFjYaRxKFiu+HJsphbuye8SEbrqIUP+FFxCeRzVIlR/mw+Jzd3cPHkZb91ztfJjtfeO4P4OLTM0BX6d3qtdIWyuXEOCOQVlhmuY7FGgwddZlwGL3kll/gd5GDsDoj7MGTHIwvpIRRRjptbbCFiXw80+QFtLq+rnCg9cLhpKrwIr6qzJZsAsshmVe+nloQe4VsKGA7tVmPVsWaDTV/6DWqQroODum5kKQxnmPv8yFsJA2gwPnnr5uQtJf2i8GwcK1uNdmozBvIA4tuPLtnlEuBMRC/yw0EGXcorMHJMdPO4Ju6JrWaSySXk71zVaa/Gj3T0VnXSuqRUCgYEA7xbB1U11ACbCDImIFTiCHbW+8fFm15AQWuvFJn8yL0vsvGAjCDoijlHKwbcmmPDlQyCXgBvCH1aYladRqYEs21AojqCu1pDQ2JSNWipqRTnySBmpfPPzsY2UhrGzbiNvNTBehOoWhmXewIfGgF+3/P/guFA+kA0FhSKmHpyNUTcCgYEAnnNVu2RyaXddJn8Urww0UEaT5gLEcB6kUDtBiE4+zZZFCQamOMFMP5daChQVspU6jTt3KiFQKe6/swx8h52i6Dw4/W9XuZfhIlhUO6bZUyjyJuIvT08ax/z7bSNB7QhkUElGy+LHcwB5Ar55oxKTRMjGjZZDzRbfxCaT97QgabcCgYAzs5we4C0Y50i3JVaEIEAi5JZyA13V55Vh0/JCLOdRSv3ajn+E2+11EULjORPjptQs+/NHBLmpxTQhyJjkKsbkdiUPErg8NfyzmQwoH1vztGAsL0BzgN2F27PunKbMfmUh/1+xJl3Qml7dC39la02CLGMP+GpPBEHZVHDP1dLJaQKBgQCbAfef4C8xT+1XyMCCUq0mzzTGsnRbvw490wpT/z/fPZ+BqfiY0zj1iOozq9xiHSzvl3oHRBm5O/Bbh96XiTYifS+/dtpaHOPJBqI4zQwUF511PpbLvnZTqQCKjxBig3AnPtg8gzldHtxDO/Wg+34Ir2XWg2cGztbPfHIKxDNlVwKBgHqOgjx3lm5PPD8yBJXjcQ0dwhIJRMk4dukpJ17EaiDNg/Je5pQNdUV0pMwE5CNsEz71yprQyeEg9QVfjltCpH37atIs1W/69pQxMwegasbgVJv4nQP31K8TIlaq6aNkgFFhG6/V/hvEvGu4kBecP5oUGsW7NLFPmkg0jNcyzbjp";
    // 支付宝公钥,查看地址：https://openhome.alipay.com/platform/keyManage.htm 对应APPID下的支付宝公钥。
    private  String alipay_public_key = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAiYuRV5uE34JpFb+4QrHx4S3x/cwIFiWrgxZ/Cnacpa4az5hog6T9uwQoch1DnZDZ8WruX4QBzoKfIU/YuJqkmXybCJhCuCszPcN22Ns923Thp9kLqYGvozrNzqi+geCZEr78xI1xq20wVfhKsTh1ES3kgkG90SnAIqp8agDxdE4GjGVV+38CVSQXJSEqrcYc9ygxlK2HZv9jnOUU9FWRbcu2OXWPfnYgkOpyxDi0KbAy3MeNROmf6BOnJ1UV2eJT5xxNg8FJURKr797oIXTnbRqolIbFwhOyElBlOD7aEMBOL1VYGEzHH28+mLXPzfawn5+w9fguxWiOrNS6KCYSwQIDAQAB";
    // 服务器[异步通知]页面路径  需http://格式的完整路径，不能加?id=123这类自定义参数，必须外网可以正常访问
    // 支付宝会悄悄的给我们发送一个请求，告诉我们支付成功的信息
    private  String notify_url = "http://order.vipgz4.91tunnel.com/payed/notify";

    // 页面跳转同步通知页面路径 需http://格式的完整路径，不能加?id=123这类自定义参数，必须外网可以正常访问
    //同步通知，支付成功，一般跳转到成功页
    private  String return_url = "http://member.gulimall.com/memberOrder.html";

    // 签名方式
    private  String sign_type = "RSA2";

    // 字符编码格式
    private  String charset = "utf-8";

    private String timeout = "30m";

    // 支付宝网关； https://openapi.alipaydev.com/gateway.do
    private  String gatewayUrl = "https://openapi.alipaydev.com/gateway.do";

    public  String pay(PayVo vo) throws AlipayApiException {

        //AlipayClient alipayClient = new DefaultAlipayClient(AlipayTemplate.gatewayUrl, AlipayTemplate.app_id, AlipayTemplate.merchant_private_key, "json", AlipayTemplate.charset, AlipayTemplate.alipay_public_key, AlipayTemplate.sign_type);
        //1、根据支付宝的配置生成一个支付客户端
        AlipayClient alipayClient = new DefaultAlipayClient(gatewayUrl,
                app_id, merchant_private_key, "json",
                charset, alipay_public_key, sign_type);

        //2、创建一个支付请求 //设置请求参数
        AlipayTradePagePayRequest alipayRequest = new AlipayTradePagePayRequest();
        alipayRequest.setReturnUrl(return_url);
        alipayRequest.setNotifyUrl(notify_url);

        //商户订单号，商户网站订单系统中唯一订单号，必填
        String out_trade_no = vo.getOut_trade_no();
        //付款金额，必填
        String total_amount = vo.getTotal_amount();
        //订单名称，必填
        String subject = vo.getSubject();
        //商品描述，可空
        String body = vo.getBody();

        alipayRequest.setBizContent("{\"out_trade_no\":\""+ out_trade_no +"\","
                + "\"total_amount\":\""+ total_amount +"\","
                + "\"subject\":\""+ subject +"\","
                + "\"body\":\""+ body +"\","
                + "\"timeout_express\":\""+timeout+"\","
                + "\"product_code\":\"FAST_INSTANT_TRADE_PAY\"}");

        String result = alipayClient.pageExecute(alipayRequest).getBody();

        //会收到支付宝的响应，响应的是一个页面，只要浏览器显示这个页面，就会自动来到支付宝的收银台页面
        System.out.println("支付宝的响应："+result);

        return result;

    }
}
